//
//  AccountView.m
//  EPTool
//
//  Created by guoshiqing on 2018/4/15.
//  Copyright © 2018年 云数动能科技. All rights reserved.
//

#import "AccountView.h"
#import "Macro.h"
#import "UIView+DDAddition.h"
#import "EPColor.h"
#import "UITextField+Format.h"
#import "StringUtil.h"

@interface AccountView () <UITextFieldDelegate>

@property (nonatomic, strong) UILabel *lblTile;
@property (nonatomic, strong) UILabel *lblLine;
@property (nonatomic, strong) UITextField *txfAccount;
@property (nonatomic, strong) AreaCodeView *viwAreaCode;

@property (nonatomic, copy) NSString *strAreaCode;
@property (nonatomic, copy) NSString *strMobile;
@end

@implementation AccountView


- (instancetype)initWithFrame:(CGRect)frame andAreaCode:(NSString *)strAreaCode andMobile:(NSString *)strMobile
{
    if (self = [super initWithFrame:frame]) {
        self.strAreaCode = strAreaCode;
        self.strMobile = strMobile;
        [self addSubviews];
        [self resetSubView];
    }
    return self;
}

- (void)addSubviews
{
    [self addSubview:self.lblTile];
    
    // 设置国家地区码
    [self addSubview:self.viwAreaCode];
    
    // 竖线
    self.lblLine.centerY = self.viwAreaCode.centerY;
    [self addSubview:self.lblLine];
    
    // 设置手机号码输入框
    [self addSubview:self.txfAccount];
    self.txfAccount.centerY = self.viwAreaCode.centerY;
}

- (void)resetSubView
{
    self.lblLine.left = self.viwAreaCode.right + 10.0f;
    self.txfAccount.left = self.lblLine.right + Cell_X;
    self.txfAccount.width = self.width - self.txfAccount.left;
}

- (UILabel *)lblTile
{
    if (!_lblTile) {
        _lblTile = [[UILabel alloc] initWithFrame:CGRectMake(0, 13.0f, 100.0f, 17.0f)];
        _lblTile.textAlignment = NSTextAlignmentLeft;
        _lblTile.font = SystemFont(12.0f);
        _lblTile.text = @"手机号码";
    }
    return _lblTile;
}

- (UILabel *)lblLine
{
    if (!_lblLine) {
        _lblLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0.5, 20.0f)];
        _lblLine.backgroundColor = [EPColor epColor_UnderLine];
    }
    return _lblLine;
}

- (AreaCodeView *)viwAreaCode
{
    if (!_viwAreaCode) {
        _viwAreaCode = [[AreaCodeView alloc] initWithFrame:CGRectMake(0, self.lblTile.bottom, 60.0f, 50.0f) withDefaultAreaCode:self.strAreaCode];
        
        // 重新选择国家地区码以后
        WEAKSELF
        _viwAreaCode.selectBlock = ^(NSString *areaCode, NSString *areaName) {
        
            // 此处逻辑可优化，如果国家地区码跟上次一样，不需要变换格式
            if ([areaCode isEqualToString:@"86"]) {
                weakSelf.txfAccount.text = [StringUtil stringFormat334:[weakSelf.txfAccount.text stringByReplacingOccurrencesOfString:@" " withString:@""]];
            }
            else
            {
                weakSelf.txfAccount.text = [StringUtil stringFormat44:[weakSelf.txfAccount.text stringByReplacingOccurrencesOfString:@" " withString:@""]];
            }

            weakSelf.strAreaCode = areaCode;
            if (weakSelf.areaCodeChangeBlock) {
                weakSelf.areaCodeChangeBlock(areaCode, weakSelf.strMobile);
            }
            [weakSelf resetSubView];
        };
    }
    return _viwAreaCode;
}


- (UITextField *)txfAccount
{
    if (!_txfAccount) {
        _txfAccount = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, LoginVC_W-80.0f, 25.0f)];
        _txfAccount.delegate = self;
        _txfAccount.placeholder = @"请输入手机号";
        _txfAccount.clearButtonMode =UITextFieldViewModeWhileEditing;
        _txfAccount.autocorrectionType = UITextAutocorrectionTypeNo;
        _txfAccount.keyboardType = UIKeyboardTypeNumberPad;
        _txfAccount.font = SystemFont(18.0f);
        [_txfAccount setValue:SystemFont(18.0f) forKeyPath:@"_placeholderLabel.font"];
        // 设置删除按钮
        _txfAccount.rightView = [self clearButtonClick];
        _txfAccount.rightViewMode = UITextFieldViewModeWhileEditing;
        _txfAccount.text = self.strMobile;
    }
    
    // 初始化不设置格式，否则退出时退格一位手机号码会按照设置的格式进行退位
    if ([self.strAreaCode isEqualToString:@"86"]) {
        _txfAccount.textFormatType = kHBTextFieldTypeFormatCNPhoneNO;
    }
    else
    {
        _txfAccount.textFormatType = kHBTextFieldTypeFormatHKPhoneNO;
    }
    
    return _txfAccount;
}


#pragma mark - 自定义清除用户名按钮
- (UIButton *)clearButtonClick
{
    UIButton *rightView = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    [rightView setImage:[UIImage imageNamed:@"login_clear"] forState:UIControlStateNormal];
    [rightView setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, -20)];
    [rightView addTarget:self action:@selector(clearEditClick:) forControlEvents:UIControlEventTouchUpInside];
    return rightView;
}

- (void)clearEditClick:(id)sender
{
    self.txfAccount.text = @"";
    if(self.btnClearBlock) self.btnClearBlock();
}


#pragma mark - UITextFieldDelegate
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    // 限定输入位数
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    NSInteger becomeLength = existedLength - selectedLength + replaceLength;
    
    NSString *currentString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (self.txfBecomeBlock) {
        self.txfBecomeBlock(becomeLength, currentString, self.strAreaCode);
    }
    return YES;
}

#pragma mark - 对外接口
- (NSString *)getAccount
{
    return self.txfAccount.text;
}

- (NSString *)getAreaCode
{
    return self.viwAreaCode.strAreaCode;
}

#pragma mark - 设置文字的颜色
- (void)setTextColor:(UIColor *)textColor
{
    self.lblTile.textColor = textColor;
//    self.lblLine.backgroundColor = textColor;
    [self.txfAccount setValue:[textColor colorWithAlphaComponent:0.5] forKeyPath:@"_placeholderLabel.textColor"];
    self.txfAccount.textColor = textColor;
    self.viwAreaCode.textColor = textColor;
}

@end
